package com.baiyang.server.service.read.impl;


import com.baiyang.server.model.article.Article;
import com.baiyang.server.model.read.ReadRecord;
import com.baiyang.server.service.article.ArticleService;
import com.baiyang.server.service.impl.BaseServiceImpl;
import com.baiyang.server.service.read.ReadRecordService;
import com.baiyang.server.tools.DataTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baiyang.server.dao.read.CommentsDao;
import com.baiyang.server.model.read.Comments;
import com.baiyang.server.service.read.CommentsService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author XVX
 * BY: @Primary 注解帮助生命@Autowired优先注入的实列
 */
@Service	
public class CommentsServiceImpl extends BaseServiceImpl<CommentsDao, Comments> implements CommentsService{

    @Autowired
    private CommentsDao commentsDao;
    @Autowired
    private ReadRecordService readRecordService;
    @Autowired
    private ArticleService articleService;

    /**
     * 更新阅读记录的评分
     * 记录评论内容
     * @param comments
     */
    @Transactional
    @Override
    public void addComment(Comments comments) {
        ReadRecord rr = new ReadRecord();
        rr.setArticleId(comments.getArticleId());
        rr.setUserId(comments.getUserId());
        rr = readRecordService.selectOne(rr);
        if(rr != null){
            rr.setScore((Integer.valueOf ((int)(comments.getScore()*10))));
            readRecordService.updateById(rr);
        }
        commentsDao.insert(comments);
    }

    @Override
    public List<Comments> getCommentRecord(String userId, int pageStart, int pageSize) {
        return commentsDao.getCommentRecord(userId,pageStart,pageSize);
    }

    @Override
    public List<Comments> getComment(String authorId) {
        Article article = new Article();
        article.setAuthorId(authorId);
        article.setState("2");
        List<Article> articles = articleService.selectList(article);
        List<String> ids = new ArrayList<>();
        articles.forEach(e->{
            ids.add(e.getId());
        });
        if(DataTool.isBlank(ids)){
            return null;
        }
        return commentsDao.getNewComment(ids,20);
    }
}
